import string

# Make a reusable string of all characters
_allchars = string.maketrans('', '')

def makefilter(keep):
    """ Return a functor that takes a string and returns a partial copy of that
    string consisting of only the characters in 'keep'.
    """

    # Make a string of all characters that are not in 'keep'
    delchars = _allchars.translate(_allchars, keep)

    # Return the functor, binding the two strings as default args
    return lambda s, a=_allchars, d=delchars: s.translate(a, d)

def canonicform(keep):
    """ Given a string, considered as a set of characters, return the
    string's characters as a canonic-form string: alphabetized
    and without duplates.
    """

    return makefilter(keep)(_allchars)

if __name__ == '__main__':
    identifier = makefilter(string.letters + string.digits + '_')
    print identifier(_allchars)
